let that = null,
	app = getApp(),
	config = app.globalData.config,
	cache = config.storage,
	helper = app.globalData.helper;
import { post } from '@/common/js/http.js';
import Index1 from './components/index1.vue';
import Index2 from './components/index2.vue';

export default {
	components: {
		Index1,
		Index2
	},
	data() {
			return {
				isLoading: true,
				index_page_set: {}
			};
		},
	onLoad(options) {
		app.globalData.showShareMenu();
		that = this;
		that.checkScene();
		that.checkAgent(options);
		that.getPageConfig();
	},
	onShow(options) {
		},
	onShareAppMessage() {
		return app.globalData.getShareConfig();
	},
	methods: {
		// 获取页面配置
		async getPageConfig() {
			try {
				let res = await post('appConfig/get', { config_type: 'index_page_set' });
				that.index_page_set = res.data;

				app.globalData.appIsAudit = that.index_page_set.index_audit_version == config.appVersion;

			} catch (error) {
				console.error('获取页面配置失败', error);
			}
		},

		/**
		 * 处理场景值信息
		 * @description 当场景值不为0时，获取用户登录凭证并上报场景信息
		 */
		checkScene() {
			let scene = app.globalData.scene
			if (scene == 0) {
				return
			}

			//#ifdef MP-WEIXIN
			uni.login({
				success: (res) => {
					post('user/scene', {
							code: res.code,
							scene: scene
						})
						.then((res) => {
							console.log(res);
						}).catch((res) => {
							console.log(res);
						});
				}
			});
			//#endif
		},

		/**
		 * 处理代理信息记录
		 * @param {Object} options - 页面参数对象
		 * @param {string} options.agent_id - 代理ID
		 * @description 当存在代理ID时，获取用户登录凭证并记录代理关系
		 */
		checkAgent(options) {
			if (helper.isSet(options.agent_id)) {
				let agent_id = options.agent_id;
				if (agent_id) {
					uni.login({
						success: (e) => {
							let code = e.code
							post('user/share', {
									code: code,
									agent_id: agent_id
								})
								.then(function(res) {
									console.log(res);
								});
						}
					});
				}
			}
		},

		

	}
};